<%@page import="dao.UserData"%>
<%@page import="java.util.Iterator"%>
<%@page import="model.Item"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.TreeMap"%>
<%@page import="java.text.NumberFormat"%>
<%@page import="model.Cart"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Túi Hàng </title>
</head>
<body>
		<%
		String email = (String) session.getAttribute("email");
		if(email==null){
			response.sendRedirect("index.jsp");
		}
		double moneyAccount = UserData.getMoney(email);
		Cart cart = null;
		if (session.getAttribute("cart") != null) {
			cart = (Cart) session.getAttribute("cart");
		}else{
			cart = new Cart();
		}
		NumberFormat nf = NumberFormat.getInstance();
		TreeMap<Item ,Integer> list = cart.getListItem();
		Iterator<Item> key = list.keySet().iterator();
		ArrayList<Double> p = new ArrayList<Double>();
		int i=list.size();
		double sum=0;
	%>
	<h1>
		Bạn đã mua
		<%=i%>
		các Vật phẩm sau:
	</h1>
	<table border="1">
		<tr>
			<b>
				<td style="width: 50%">TÊN Vật Phẩm</td>
				<td style="width: 15%">SỐ LƯỢNG</td>
				<td style="width: 15%">ĐƠN GIÁ</td>
				<td style="width: 20%">TỔNG TIỀN</td>
			</b>
		</tr>

		<%
			while (key.hasNext()) {
				Item k = key.next();
				int quantity = list.get(k); 
				double price = k.getPriceItem();
				long id =k.getIdItem();
				p.add(quantity * price);
		%>
		<tr>
			<td><%=k.getNameItem() %></td>
			<form action="CalCart" method="post">
				<input type="hidden" name="id" value="<%=id%>">
				<td><input type="text" name="quantity" value="<%=quantity%>"
					size="5"></td>
				<td><input type="text" name="price" value="<%=price%>"></td>
				<td align="right"><%= nf.format(p.get(p.size()-1))%></td>
				<td>
					<button type="submit">Cập nhật</button>
				</td>
			</form>
			<%
				}
			%>
		</tr>

		<%
			for (double y : p) {
				sum+=y;
			}
		%>
		<tr>
			<b>
				<td colspan="2">TỔNG THÀNH TIỀN</td>
				<td><%=nf.format(sum)%> xu</td>  Số tiền xu của bạn : <%=moneyAccount %>
		</tr>
		</b>
	</table>
	<br>
	<a href="shopAvatar.jsp"><button>Tiếp tục mua sách</button></a>
	<%if(sum > moneyAccount){ %>
	<h2><b>Bạn Không đủ tiền để Mua vật Phẩm</b></h2>
	<%}else{ %>
	<a href="SuccessOrder?sum=<%=sum%>"><button>Thanh toán</button></a>
		<%} %>
</body>
</html>